import serial
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QMessageBox

from common.common import Common
from data_manage.com_manager import ComManager


class ShowTabCom:
    def __init__(self, ui, ptr):
        self.ui = ui
        self.b_open_com = False
        self.ptr = ptr
        pass

    def init_ui(self):
        png = Common.get_resource_path() + "提示.png"
        self.ui.toolButton_3.setIcon(QIcon(png))
        png = Common.get_resource_path() + "com_port.png"
        self.ui.toolButton.setIcon(QIcon(png))
        png = Common.get_resource_path() + "yellow.png"
        self.ui.toolButton_4.setIcon(QIcon(png))
        self.ui.toolButton_5.setIcon(QIcon(png))
        png = Common.get_resource_path() + "baud.png"
        self.ui.toolButton_2.setIcon(QIcon(png))
        self.ui.comboBox_baud.addItems(["4800", "9600", "14400", "19200", "115200"])
        self.ui.comboBox_baud.setCurrentIndex(4)
        self.show_ports()
        self.ui.pushButton_open_port.clicked.connect(lambda: self.btn_open_port_clicked("_open_port_"))
        self.ui.pushButton_recv_data.clicked.connect(lambda: self.btn_recv_data_clicked("_recv_data_"))
        pass

    def show_ports(self):
        port_dict = ComManager.get_com_port()
        self.ui.comboBox_com_port.clear()
        # if len(port_dict) == 0:
        #     self.ui.statusbar.showMessage("No com port!!!")
        # else:
        #     self.ui.comboBox_com_port.addItems(port_dict.keys())
        pass

    def btn_open_port_clicked(self, string):
        print(string)
        QMessageBox.warning(self.ptr, "Error", "还没做，别试了", QMessageBox.Cancel)
        # QMessageBox.warning(self, "Error", "不存在Json文件或文件中没有数据", "")
        # port_dict = ComManager.get_com_port()
        # if len(port_dict) == 0:
        #     QMessageBox.warning(self, "Error", "不存在Json文件或文件中没有数据")
        #     return
        # ComManager().init_com(port_dict[self.ui.cobox_com_sta.currentText()],
        #                       int(self.ui.cobox_baud_sta.currentText()),
        #                       serial.PARITY_NONE, 1, 8)
        # if self.b_open_com:  # open
        #     ComManager().open_com_port()
        #     self.ui.pushButton_open_port.setText("关闭串口")
        #     if ComManager().check_com_open():
        #         self.change_png(self.ui.t_btn_baud_sta)
        #         self.change_png(self.ui.t_btn_com_sta)
        #     else:
        #         self.ui.label_info.setText("请再次打开串口")
        #     self.b_open_com = False
        # else:  # close
        #     # Common().set_rev_loop(False)
        #     if ComManager().open_com_port():
        #         ComManager().close_com_port()
        #     self.ui.btn_open_port_sta.setText("打开串口")
        #     self.change_png(self.ui.t_btn_baud_sta)
        #     self.change_png(self.ui.t_btn_com_sta)
        #     self.b_open_com = True
        pass

    def btn_recv_data_clicked(self, string):
        print(string)
        QMessageBox.warning(self.ptr, "Error", "还没做，别试了", QMessageBox.Cancel)